
//
//  souSuoViewController.m
//  yiyou
//
//  Created by tangtangchitangtang on 2017/8/16.
//  Copyright © 2017年 tangtangchitangtang. All rights reserved.
//

#import "souSuoViewController.h"
#import "souSuoView.h"
#import "TitleView.h"
@interface souSuoViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)souSuoView *aView;
@property(nonatomic,strong)UITextField *souShuoLabel;
@property(nonatomic,strong)UIView *liSiView;
@property(nonatomic,strong)  NSArray *sousuoArr;
@end

@implementation souSuoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (_abc==1) {
        _sousuoArr=[[NSUserDefaults standardUserDefaults] objectForKey:@"搜索1"];
    }
    if (_abc==2)
    {
        _sousuoArr=[[NSUserDefaults standardUserDefaults] objectForKey:@"搜索2"];
    }
    if (_abc==3)
    {
        _sousuoArr=[[NSUserDefaults standardUserDefaults] objectForKey:@"搜索3"];
    }
    if (_abc==4)
    {
        _sousuoArr=[[NSUserDefaults standardUserDefaults] objectForKey:@"搜索4"];
    }
    if (_abc==5)
    {
        _sousuoArr=[[NSUserDefaults standardUserDefaults] objectForKey:@"搜索5"];
    }if (_abc==6)
    {
        _sousuoArr=[[NSUserDefaults standardUserDefaults] objectForKey:@"搜索6"];
    }
    
    
    _aView=[souSuoView new];
    [self.view addSubview:_aView];
    [_aView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(0);
    }];
    _aView.abc=_abc;
    _aView.tableArr=[NSMutableArray arrayWithCapacity:0];
    if (_abc==2) {
        [_aView creatView2];
    }else{
        [_aView creatView];
    }
    
    if (_sousuoArr==nil||_sousuoArr.count==0) {
        NSLog(@"我爱你");
    }
    else{
        
        _liSiView=[UIView new];
        [self.view addSubview:_liSiView];
        [_liSiView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.mas_equalTo(0);
        }];
        _liSiView.backgroundColor=[UIColor whiteColor];
        
        UILabel *lable=[UILabel new];
        [_liSiView addSubview:lable];
        [lable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(100);
        }];
        
        lable.text=@"历史记录";
        lable.font=[UIFont systemFontOfSize:15];
        lable.textColor=ColorWithRGB(49, 49, 49);
       
        int x=_sousuoArr.count/3;
        int y=_sousuoArr.count%3;
        if (y>0) {
            x+=1;
        }
        int index=0;
        for (int i=0; i<x-1; i++) {
            for (int j=0; j<3; j++) {
                UIButton *btn=[UIButton new];
                [_liSiView addSubview:btn];
                [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo((_mainW-_mainW*0.75)/2+_mainW*0.25*j);
                    make.width.mas_equalTo(_mainW*0.25-1);
                    make.top.mas_equalTo(lable.mas_bottom).mas_equalTo(20+40*i);
                    make.height.mas_equalTo(30);
                }];
                btn.tag=index;
                [btn setTitle:_sousuoArr[index] forState:0];
                [btn setTitleColor:_backgroundColor forState:0];
                btn.titleLabel.font=[UIFont boldSystemFontOfSize:13];
                [btn addTarget:self action:@selector(creatSouSuo2:) forControlEvents:UIControlEventTouchUpInside];
                
                UIView *XianView=[UIView new];
                [_liSiView addSubview:XianView];
                [XianView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(btn);
                    make.left.mas_equalTo(btn.mas_right);
                    make.height.mas_equalTo(20);
                    make.width.mas_equalTo(1);
                }];
                XianView.backgroundColor=_backgroundColor;
                index+=1;
            }
            
        }
        for (int j=0; j<y; j++) {
            UIButton *btn=[UIButton new];
            [_liSiView addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo((_mainW-_mainW*0.75)/2+_mainW*0.25*j);
                make.width.mas_equalTo(_mainW*0.25-1);
                make.top.mas_equalTo(lable.mas_bottom).mas_equalTo(20+40*x);
                make.height.mas_equalTo(30);
            }];
            btn.tag=index;
            [btn setTitle:_sousuoArr[index] forState:0];
            [btn setTitleColor:_backgroundColor forState:0];
            btn.titleLabel.font=[UIFont boldSystemFontOfSize:13];
            [btn addTarget:self action:@selector(creatSouSuo2:) forControlEvents:UIControlEventTouchUpInside];
            UIView *XianView=[UIView new];
            [_liSiView addSubview:XianView];
            [XianView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(btn);
                make.left.mas_equalTo(btn.mas_right);
                make.height.mas_equalTo(20);
                make.width.mas_equalTo(1);
            }];
            XianView.backgroundColor=_backgroundColor;
            index+=1;

        }
        
    }
    _aView.viewController=self;
    [self creatSouSuo];

}


-(void)creatSouSuo{

    TitleView *topView=[[TitleView alloc] initWithFrame:CGRectMake(0, 10, _mainW-50, 36)];
    topView.intrinsicContentSize = CGSizeMake(_mainW-50, 36);
    self.navigationItem.titleView = topView;
    
    
    UIImageView *imageview=[UIImageView new];
    [topView addSubview:imageview];
    [imageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-20);
        make.width.height.mas_equalTo(14);
    }];
    imageview.image=[UIImage imageNamed:@"搜索"];
    imageview.userInteractionEnabled=YES;
    UITapGestureRecognizer *Market = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sousuo)];
    [imageview addGestureRecognizer:Market];
    
    _souShuoLabel=[UITextField new];
    [topView addSubview:_souShuoLabel];
    [_souShuoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(imageview.mas_left).mas_equalTo(-10);
    }];
    _souShuoLabel.placeholder=@"请输入关键词";
    _souShuoLabel.font=[UIFont systemFontOfSize:14];
    _souShuoLabel.delegate=self;
    _souShuoLabel.returnKeyType= UIReturnKeyDone;

    
}


-(IBAction)creatSouSuo2:(UIButton *)btn{
    [_souShuoLabel resignFirstResponder];
    NSMutableArray *tablArr=[NSMutableArray arrayWithArray:_sousuoArr];
    NSString *keyword=tablArr[btn.tag];
    _souShuoLabel.text=keyword;
    if (_abc==1) {
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:keyword,@"consult_content",@"1",@"page",@"10",@"pagesize", nil];
        
        [ZQTools AFNPostDataUrl:@"user/consult/select_consult" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
            [_liSiView removeFromSuperview];
            NSArray *typeArr=responseObject;
            _aView.tableArr=[NSMutableArray arrayWithArray:typeArr];
            [_aView.tableView reloadData];
        } anderrorBlock:nil];
    }
    if (_abc==2) {
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:keyword,@"keywords",[NSString stringWithFormat:@"%d",1],@"page",@"5",@"pagesize",_gtype_id2,@"gtype_one_id",@"0",@"order", nil];
        [ZQTools AFNPostDataUrl:@"user/goods/goods_list" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
             [_liSiView removeFromSuperview];
            NSArray *typeArr=responseObject;
            [_aView.tableArr addObjectsFromArray:typeArr];
            [_aView.collectionView reloadData];
            
            
        } anderrorBlock:nil];
        
    }
    if (_abc==3) {
        NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
        NSString *token=[userDic objectForKey:@"token"];
        NSNumber *waiter_id1=[userDic objectForKey:@"user_id"];
        NSString *user_id=[NSString stringWithFormat:@"%@",waiter_id1];
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:token,@"token",@"0",@"user_role",user_id,@"user_id",[NSString stringWithFormat:@"%d",1],@"page",@"5",@"pagesize",keyword,@"keywords",_city_name,@"city_name", nil];
        [ZQTools AFNPostDataUrl:@"user/waiter/get_list" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
            [_liSiView removeFromSuperview];
            NSArray *typeArr=responseObject;
            [_aView.tableArr addObjectsFromArray:typeArr];
            [_aView.tableView reloadData];

        } anderrorBlock:nil];
    }if (_abc==4) {
        NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
        NSString *token=[userDic objectForKey:@"token"];
        NSNumber *waiter_id1=[userDic objectForKey:@"user_id"];
        NSString *user_id=[NSString stringWithFormat:@"%@",waiter_id1];
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:token,@"token",@"0",@"user_role",user_id,@"user_id",[NSString stringWithFormat:@"%d",1],@"page",@"5",@"pagesize",keyword,@"search",@"0",@"ct_id", nil];
        [ZQTools AFNPostDataUrl:@"user/Question/question_list" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
            [_liSiView removeFromSuperview];
            NSArray *typeArr=responseObject;
            [_aView.tableArr addObjectsFromArray:typeArr];
            [_aView.tableView reloadData];
            
        } anderrorBlock:nil];
    }
    if (_abc==5) {
        NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
        NSString *token=[userDic objectForKey:@"token"];
        NSNumber *waiter_id1=[userDic objectForKey:@"user_id"];
        NSString *user_id=[NSString stringWithFormat:@"%@",waiter_id1];
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:token,@"token",@"0",@"user_role",user_id,@"user_id",[NSString stringWithFormat:@"%d",1],@"page",@"5",@"pagesize",keyword,@"keywords",@"0",@"ct_id", nil];
        [ZQTools AFNPostDataUrl:@"user/successful/successful" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
            [_liSiView removeFromSuperview];
            NSArray *typeArr=responseObject;
            [_aView.tableArr addObjectsFromArray:typeArr];
            [_aView.tableView reloadData];
            
        } anderrorBlock:nil];
    }if (_abc==6) {
        NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
        NSString *token=[userDic objectForKey:@"token"];
        NSNumber *waiter_id1=[userDic objectForKey:@"user_id"];
        NSString *user_id=[NSString stringWithFormat:@"%@",waiter_id1];
        
         NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:_city_name,@"city_name",token,@"token",@"0",@"user_role",user_id,@"user_id",[NSString stringWithFormat:@"%d",1],@"page",@"5",@"pagesize",[NSString stringWithFormat:@"%d",0],@"order_by",keyword,@"keywords", nil];
        [ZQTools AFNPostDataUrl:@"user/waiter/get_expert_list" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
            [_liSiView removeFromSuperview];
            NSArray *typeArr=responseObject;
            [_aView.tableArr addObjectsFromArray:typeArr];
            [_aView.tableView reloadData];
            
        } anderrorBlock:nil];
    }
}

-(void)sousuo{
    
    
    NSMutableArray *tablArr=[NSMutableArray arrayWithArray:_sousuoArr];
  
    
    [_aView.tableArr removeAllObjects];

    [_souShuoLabel resignFirstResponder];
    NSString *keyword=_souShuoLabel.text;

    
    keyword = [keyword stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    keyword=[keyword stringByReplacingOccurrencesOfString: @" " withString: @""];
    
    
    
    
    if ([keyword isEqualToString:@""]) {
        [ZQTools svpInfo:@"不能为空"];
        return;
    }
    if (_sousuoArr.count>12) {
        for (int i=0; i<_sousuoArr.count-12; i++) {
            [tablArr removeObjectAtIndex:i];
        }
    }
    
    for (int i=0; i<tablArr.count; i++) {
        NSString *typeStr=tablArr[i];
        if ([typeStr isEqualToString:keyword]) {
            [tablArr removeObjectAtIndex:i];
            continue;
        }
    }
    [tablArr addObject:keyword];
    if (_abc==1) {
         [[NSUserDefaults standardUserDefaults] setObject:tablArr forKey:@"搜索1"];
    }
    if (_abc==2) {
        [[NSUserDefaults standardUserDefaults] setObject:tablArr forKey:@"搜索2"];
    }
    if (_abc==3) {
        [[NSUserDefaults standardUserDefaults] setObject:tablArr forKey:@"搜索3"];
    }if (_abc==4) {
        [[NSUserDefaults standardUserDefaults] setObject:tablArr forKey:@"搜索4"];
    }if (_abc==5) {
        [[NSUserDefaults standardUserDefaults] setObject:tablArr forKey:@"搜索5"];
    }if (_abc==6) {
        [[NSUserDefaults standardUserDefaults] setObject:tablArr forKey:@"搜索6"];
    }
    if (_abc==1) {
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:keyword,@"consult_content",@"1",@"page",@"10",@"pagesize", nil];
        
        [ZQTools AFNPostDataUrl:@"user/consult/select_consult" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
            [_liSiView removeFromSuperview];
            NSArray *typeArr=responseObject;
            [_aView.tableArr addObjectsFromArray:typeArr];
            [_aView.tableView reloadData];
        } anderrorBlock:nil];

    }
    if (_abc==2){
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:keyword,@"keywords",[NSString stringWithFormat:@"%d",1],@"page",@"5",@"pagesize",_gtype_id2,@"gtype_one_id",@"0",@"order", nil];
        [ZQTools AFNPostDataUrl:@"user/goods/goods_list" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
            [_liSiView removeFromSuperview];
            [_aView.tableArr removeAllObjects];
            
            NSArray *typeArr=responseObject;
            [_aView.tableArr addObjectsFromArray:typeArr];
            [_aView.collectionView reloadData];
            
//            if (typeArr.count!=0) {
//                _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//                    [_collectionView.mj_footer endRefreshing];
//                    [_collectionView.mj_header endRefreshing];
//                    _page=_page+1;
//                    [self loadData];
//                }];
//            }
//            else{
//                _collectionView.mj_footer.hidden=YES;
//
//            }
            
        } anderrorBlock:nil];
    }
    if (_abc==3){
       
        NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
        NSString *token=[userDic objectForKey:@"token"];
        NSNumber *waiter_id1=[userDic objectForKey:@"user_id"];
        NSString *user_id=[NSString stringWithFormat:@"%@",waiter_id1];
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:token,@"token",@"0",@"user_role",user_id,@"user_id",_city_name,@"city_name",[NSString stringWithFormat:@"%d",1],@"page",@"5",@"pagesize",keyword,@"keywords", nil];
        [ZQTools AFNPostDataUrl:@"user/waiter/get_list" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
             [_liSiView removeFromSuperview];
            [_aView.tableArr removeAllObjects];
            
            NSArray *typeArr=responseObject;
            [_aView.tableArr addObjectsFromArray:typeArr];
            [_aView.tableView reloadData];
            
//            if (typeArr.count!=0) {
//                _aView.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//                    [_tableView.mj_footer endRefreshing];
//                    [_tableView.mj_header endRefreshing];
//                    _page=_page+1;
//                    [self loadData];
//                }];
//            }
//            else{
//                _tableView.mj_footer.hidden=YES;
//
//            }
            
        } anderrorBlock:nil];
    }

    if (_abc==4) {
        NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
        NSString *token=[userDic objectForKey:@"token"];
        NSNumber *waiter_id1=[userDic objectForKey:@"user_id"];
        NSString *user_id=[NSString stringWithFormat:@"%@",waiter_id1];
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:token,@"token",@"0",@"user_role",user_id,@"user_id",[NSString stringWithFormat:@"%d",1],@"page",@"5",@"pagesize",keyword,@"search",@"0",@"ct_id", nil];
        [ZQTools AFNPostDataUrl:@"user/Question/question_list" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
            [_liSiView removeFromSuperview];
            [_aView.tableArr removeAllObjects];
            NSArray *typeArr=responseObject;
            [_aView.tableArr addObjectsFromArray:typeArr];
            [_aView.tableView reloadData];
            
        } anderrorBlock:nil];
    }
    if (_abc==5) {
        NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
        NSString *token=[userDic objectForKey:@"token"];
        NSNumber *waiter_id1=[userDic objectForKey:@"user_id"];
        NSString *user_id=[NSString stringWithFormat:@"%@",waiter_id1];
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:token,@"token",@"0",@"user_role",user_id,@"user_id",[NSString stringWithFormat:@"%d",1],@"page",@"5",@"pagesize",keyword,@"keywords",@"0",@"ct_id", nil];
        [ZQTools AFNPostDataUrl:@"user/successful/successful" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
            [_liSiView removeFromSuperview];
            [_aView.tableArr removeAllObjects];
            NSArray *typeArr=responseObject;
            [_aView.tableArr addObjectsFromArray:typeArr];
            [_aView.tableView reloadData];
            
        } anderrorBlock:nil];
    } if (_abc==6) {
        NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];
        NSString *token=[userDic objectForKey:@"token"];
        NSNumber *waiter_id1=[userDic objectForKey:@"user_id"];
        NSString *user_id=[NSString stringWithFormat:@"%@",waiter_id1];
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:_city_name,@"city_name",token,@"token",@"0",@"user_role",user_id,@"user_id",[NSString stringWithFormat:@"%d",1],@"page",@"5",@"pagesize",[NSString stringWithFormat:@"%d",0],@"order_by",keyword,@"keywords", nil];
        [ZQTools AFNPostDataUrl:@"user/waiter/get_expert_list" Dict:dic andTableView:nil andViewController:self andSuccessBlock:^(id responseObject) {
            [_liSiView removeFromSuperview];
            [_aView.tableArr removeAllObjects];
            NSArray *typeArr=responseObject;
            [_aView.tableArr addObjectsFromArray:typeArr];
            [_aView.tableView reloadData];
            
        } anderrorBlock:nil];
    }


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string{
    
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_souShuoLabel resignFirstResponder];
    [self sousuo];
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
